package com.socialsoftware.ui.Discover;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.socialsoftware.R;
import com.socialsoftware.databinding.FragmentDiscoverBinding;
import com.socialsoftware.adapter.MomentsAdapter;
import com.socialsoftware.model.Moment;

public class DiscoverFragment extends Fragment {

    private FragmentDiscoverBinding binding;
    private DiscoverViewModel discoverViewModel;
    private MomentsAdapter momentsAdapter;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        discoverViewModel = new ViewModelProvider(this).get(DiscoverViewModel.class);

        binding = FragmentDiscoverBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        setupRecyclerView();
        setupSwipeRefresh();
        setupFab();

        // Observe moments data from ViewModel
        discoverViewModel.getMoments().observe(getViewLifecycleOwner(), moments -> {
            momentsAdapter.setMoments(moments);
        });

        return root;
    }

    private void setupRecyclerView() {
        RecyclerView recyclerView = binding.recyclerMoments;
        recyclerView.setLayoutManager(new LinearLayoutManager(requireContext()));
        
        // Create adapter with click listeners
        momentsAdapter = new MomentsAdapter(
            requireContext(),
            moment -> {
                // Handle moment click
                Toast.makeText(requireContext(), "点击了朋友圈内容", Toast.LENGTH_SHORT).show();
            },
            (moment, position) -> {
                // Handle like click
                discoverViewModel.toggleLike(moment, position);
            },
            (moment, position) -> {
                // Handle comment click
                showCommentDialog(moment, position);
            }
        );
        
        recyclerView.setAdapter(momentsAdapter);
    }

    private void setupSwipeRefresh() {
        SwipeRefreshLayout swipeRefreshLayout = binding.swipeRefreshLayout;
        swipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary);
        swipeRefreshLayout.setOnRefreshListener(() -> {
            // Refresh data
            discoverViewModel.refreshMoments();
            swipeRefreshLayout.setRefreshing(false);
        });
    }

    private void setupFab() {
        FloatingActionButton fab = binding.fabPost;
        fab.setOnClickListener(v -> {
            // Open post creation screen
            Toast.makeText(requireContext(), "发布朋友圈", Toast.LENGTH_SHORT).show();
        });
    }

    private void showCommentDialog(Moment moment, int position) {
        // Show comment input dialog
        Toast.makeText(requireContext(), "添加评论", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}